package com.assignment.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JButton;

import com.assignment.database.DBOperations;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class LoginScreen extends JFrame implements KeyListener {

	private JPanel contentPane;
	private JTextField userText;
	private JPasswordField passText;
	JButton btnLogin;
	JFrame thisFrame = this;

	
	public LoginScreen() {
		
		
		addKeyListener(this);
		setFocusable(true);
		setTitle("Chesney's Fireplaces");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(450, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.setBounds(27, 75, 377, 176);
		contentPane.add(panel);
		panel.setLayout(null);
		
		userText = new JTextField();
		userText.setBounds(194, 39, 108, 23);
		userText.addKeyListener(this);
		panel.add(userText);
		userText.setColumns(10);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(85, 43, 72, 14);
		panel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(85, 86, 72, 14);
		panel.add(lblPassword);
		
		passText = new JPasswordField();
		passText.setBounds(194, 82, 108, 23);
		passText.addKeyListener(this);
		panel.add(passText);
		
		 btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String user = userText.getText();
				String pass = passText.getText();
				
				boolean b = DBOperations.checkUserCredentials(user, pass);
				
				if (b) {
					new SearchScreen().setVisible(true);
					thisFrame.dispose();
				}
				else {
					JOptionPane.showMessageDialog(thisFrame, "Incorrect Username or Password");
				}
			}
		});
		btnLogin.setBounds(96, 129, 89, 23);
		panel.add(btnLogin);
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnExit.setBounds(209, 129, 89, 23);
		panel.add(btnExit);
		
		JLabel lblLogin = new JLabel("Login");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblLogin.setBounds(188, 22, 63, 34);
		contentPane.add(lblLogin);
	}

	@Override
	public void keyPressed(KeyEvent k) {
		
		if (k.getKeyCode()==KeyEvent.VK_ENTER) {
			
		for (ActionListener a : btnLogin.getActionListeners())
			a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
